import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;

/**
 * DestroyerGameScreen Class
 * This is a subclass of WordGameScreen, simply used to show
 * a particular game in which the player destroys a number of tiles
 *  
 * @author Zong Xiong
 */
public class DestroyerGameScreen extends WordGameScreen {

/***************************************************
 * Fields
 ***************************************************/
	
	// number of tiles to destroy to win
	private int TILE_GOAL = 10;  
	// used to store and load scores
	private Preferences prefs;
	// tracks score placement after goal is met
	private int scorePlacement = 0;
	
/***************************************************
 * Public Methods
 ***************************************************/

	/**
	 * Default Constructor
	 * calls super-class constructor then own initialization
	 */
	public DestroyerGameScreen() {
		super();
		destroyGameInitialize();
	}
	
	/**
	 * Constructor with parent Game
	 * calls super-class constructor then own initialization
	 * @param inGame , parent Game
	 */
	public DestroyerGameScreen(Game inGame) {
		super();
		prefs = Gdx.app.getPreferences("DestroyerWordGame");
	}
	
/***************************************************
 * Protected Methods
 ***************************************************/
	
	/**
	 * Modified subclass draw method
	 * Adds a line of text for score display
	 */
	protected void draw() {
		super.draw();
		printBoard("Tiles:" + (TILE_GOAL-tilesScore) + "   Time:" + gameTimer.printTime() , 20, 79);
	}
	
	/**
	 * Modified update method
	 * Tracks if goals are met for game and if so ends game
	 */
	protected void update() {
		super.update();
		
		/* If tiles required are destroyed, then we check placement on score charts,
		 * if the score charts, we update the charts and prompt player to enter in
		 * a name to put on score chart */
		if (tilesScore >= TILE_GOAL) {
			super.loseGame();
			
			// checks if player charted
			for (int i=1; i<6 && scorePlacement == 0; i++) {
				if (gameTimer.getTime() < prefs.getLong("Place" + i + "Score")) {
					scorePlacement = i;
					shiftScores(i);
					prefs.putLong("Place" + i + "Score", gameTimer.getTime());
				}
			}
			
			// prompts user to name only if player charted
			if (scorePlacement > 0) {
				super.enterHighScore();
				prefs.putString("Place" + scorePlacement + "Name", super.getScoreName() );
				prefs.flush();
			}
		}
	}

/***************************************************
 * Private Methods
 ***************************************************/
	
	/**
	 * ShiftScores
	 * shifts scores downward from scorePlacement
	 * @param scorePlacement, place acquired by player
	 */
	private void shiftScores(int scorePlacement) {
		for (int i = 5; i > scorePlacement; i--) {
			prefs.putLong("Place" + (i) + "Score", prefs.getLong("Place" + (i-1) + "Score"));
			prefs.putString("Place" + (i) + "Name", prefs.getString("Place" + (i-1) + "Name"));
		}
	}
	
	/**
	 * destroyGameInitialize
	 * initialization for a destroyer game
	 */
	private void destroyGameInitialize() {
		// load score board
		prefs = Gdx.app.getPreferences("DestroyerWordGame");

	}
}
